Skip to content

feat: add Model::firstOrInsert() with failure handling#10012

Merged
michalsn merged 5 commits intocodeigniter4:4.8from
michalsn:feat/first-or-insert
Mar 2, 2026
Merged

feat: add Model::firstOrInsert() with failure handling#10012
michalsn merged 5 commits intocodeigniter4:4.8from
michalsn:feat/first-or-insert

Conversation

@michalsn
Copy link
Copy Markdown
Member

Description
This PR adds Model::firstOrInsert(), which finds the first row matching the given $attributes, or inserts a new row using the merged result of $attributes and $values. Both parameters accept an array, stdClass, or an Entity.

Closes #5163

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@michalsn michalsn added enhancement PRs that improve existing functionalities 4.8 PRs that target the `4.8` branch. labels Feb 27, 2026
Comment thread system/Model.php
Comment thread user_guide_src/source/models/model/065.php
@michalsn michalsn merged commit c82e68e into codeigniter4:4.8 Mar 2, 2026
50 checks passed
@michalsn
Copy link
Copy Markdown
Member Author

michalsn commented Mar 2, 2026

Thank you for the reviews!

@michalsn michalsn deleted the feat/first-or-insert branch March 2, 2026 12:37
JulianAtkins pushed a commit to JulianAtkins/CodeIgniter4 that referenced this pull request Mar 19, 2026
Copy link
Copy Markdown
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though I’m late in getting here, I’m grateful for this one it turned out great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants